package tdh.xt.common.utils;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tdh.xt.entity.po.User;
import tdh.xt.entity.vo.Page;

import java.util.List;

public class CommonUtil {

    static Logger logger = LoggerFactory.getLogger(CommonUtil.class);

    public static String getXmlStringFromUser(User user) {
        StringBuilder stringBuilder = new StringBuilder("<?xml version='1.0' encoding='UTF-8'?>");
        stringBuilder.append("<rows>");

        try {
            stringBuilder.append("<userdata name=\"totalnumber\">1</userdata>");
            if (user != null) {
                stringBuilder.append("<row id=\"1\">")
                        .append("<cell>1</cell>")
                        .append("<cell>").append(user.getYhxm()).append("</cell>")
                        .append("<cell>").append(user.getYhid()).append("</cell>")
                        .append("<cell>").append(user.getYhbm()).append("</cell>")
                        .append("<cell>").append(user.getYhxb()).append("</cell>")
                        .append("<cell><![CDATA[")
                        .append("<a class='tdh_btn tdh_btn_white mybtn' onclick=doShow(\"").append(user.getYhdm()).append("\")><i class=\"tdh_icon " +
                        "icon_batch\"></i>查看</a>")
                        .append("<a class='tdh_btn tdh_btn_blue mybtn' onclick=doUpdate(\"").append(user.getYhdm()).append("\")><i " +
                        "class=\"tdh_icon " +
                        "icon_edit\"></i>更新</a>")
                        .append("<a class='tdh_btn tdh_btn_yellow mybtn' onclick=doDelete(\"").append(user.getYhdm()).append("\")><i class=\"tdh_icon icon_del\"></i>删除</a>")
                        .append("]]></cell>")
                        .append("</row>");
            }
        } catch (Exception e) {
            logger.error("==getXmlStringFromUser:{}" + JSON.toJSONString(user));
        } finally {
            stringBuilder.append("</rows>");
        }
        return stringBuilder.toString();
    }

    public static String getXmlStringFromPageUser(Page<User> pages) {
        StringBuilder stringBuilder = new StringBuilder("<?xml version='1.0' encoding='UTF-8'?>");
        stringBuilder.append("<rows>");

        try {
            List<User> users = pages.getData();
            int id = 1;
            stringBuilder.append("<userdata name=\"totalnumber\">").append(pages.getTotalCount()).append("</userdata>");
            for (User user : users) {
                stringBuilder.append("<row id=\"").append(id).append("\">")
                        .append("<userdata name=\"yhdm\">").append(user.getYhdm()).append("</userdata>")
                        .append("<cell>").append(id++).append("</cell>")
                        .append("<cell>").append(user.getYhxm()).append("</cell>")
                        .append("<cell>").append(user.getYhid()).append("</cell>")
                        .append("<cell>").append(user.getYhbm()).append("</cell>")
                        .append("<cell>").append(user.getYhxb()).append("</cell>")
                        .append("<cell><![CDATA[")
                        .append("<button class='tdh_btn tdh_btn_white mybtn' onclick=doShow(\"").append(user.getYhdm()).append("\")><i class=\"tdh_icon " +
                                "icon_batch\"></i>查看</button>")
                        .append("<button class='tdh_btn tdh_btn_blue mybtn' onclick=doUpdate(\"").append(user.getYhdm()).append("\")><i " +
                        "class=\"tdh_icon " +
                                "icon_edit\"></i>更新</button>")
                        .append("<button class='tdh_btn tdh_btn_yellow mybtn' onclick=doDelete(\"").append(user.getYhdm()).append("\")><i class=\"tdh_icon icon_del\"></i>删除</button>")
                        .append("]]></cell>")
                        .append("</row>");
            }
        } catch (Exception e){
            logger.error("==getXmlStringFromPageUser:{}" + JSON.toJSONString(pages));
        } finally {
            stringBuilder.append("</rows>");
        }
        return stringBuilder.toString();
    }
}
